<%@ tag language="java" pageEncoding="UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>
<%@ attribute name="id" type="java.lang.String" required="true" description="编号" %>
<%@ attribute name="name" type="java.lang.String" required="true" description="隐藏域名称（ID）" %>
<%@ attribute name="value" type="java.lang.String" required="true" description="隐藏域值（ID）" %>
<%@ attribute name="labelName" type="java.lang.String" required="true" description="输入框名称（Name）" %>
<%@ attribute name="labelValue" type="java.lang.String" required="true" description="输入框值（Name）" %>
<%@ attribute name="title" type="java.lang.String" required="true" description="选择框标题" %>
<%@ attribute name="url" type="java.lang.String" required="true" description="树结构数据地址" %>
<%@ attribute name="checked" type="java.lang.Boolean" required="false" description="是否显示复选框，如果不需要返回父节点，请设置notAllowSelectParent为true" %>
<%@ attribute name="extId" type="java.lang.String" required="false" description="排除掉的编号（不能选择的编号）" %>
<%@ attribute name="isAll" type="java.lang.Boolean" required="false" description="是否列出全部数据，设置true则不进行数据权限过滤（目前仅对Office有效）" %>
<%@ attribute name="notAllowSelectRoot" type="java.lang.Boolean" required="false" description="不允许选择根节点" %>
<%@ attribute name="notAllowSelectParent" type="java.lang.Boolean" required="false" description="不允许选择父节点" %>
<%@ attribute name="module" type="java.lang.String" required="false" description="过滤栏目模型（只显示指定模型，仅针对CMS的Category树）" %>
<%@ attribute name="selectScopeModule" type="java.lang.Boolean" required="false" description="选择范围内的模型（控制不能选择公共模型，不能选择本栏目外的模型）（仅针对CMS的Category树）" %>
<%@ attribute name="allowClear" type="java.lang.Boolean" required="false" description="是否允许清除" %>
<%@ attribute name="allowInput" type="java.lang.Boolean" required="false" description="文本框可填写" %>
<%@ attribute name="cssClass" type="java.lang.String" required="false" description="css样式" %>
<%@ attribute name="cssStyle" type="java.lang.String" required="false" description="css样式" %>
<%@ attribute name="smallBtn" type="java.lang.Boolean" required="false" description="缩小按钮显示" %>
<%@ attribute name="dataMsgRequired" type="java.lang.String" required="false" description="" %>
<%@ attribute name="officeGrade" type="java.lang.Boolean" required="false" description="设置站点等级要求" %>
<div class="input-append">
    <div class="input-group">
        <input id="${id}Id" name="${name}" class="${cssClass}" type="hidden" value="${value}"/>
        <input id="${id}Name" name="${labelName}" ${allowInput?'':'readonly="readonly"'} type="text"
               value="${labelValue}" data-msg-required="${dataMsgRequired}" class="form-control ${cssClass}"/>
        <span class="input-group-btn">
            <button id="${id}Button" type="button" class="btn btn-primary"
                    style="${smallBtn?'padding:4px 2px;':''}"><i class="glyphicon glyphicon-search"></i></button>
        </span>
    </div>
</div>

<script type="text/javascript">
    $(function () {
        $("#${id}Name,#${id}Button").unbind('click').click(function () {
            var dialog = bootbox.dialog({
                message: getIframe("${ctx}/tag/treeselect?url=${url}&module=${module}&checked=${checked}&extId=${extId}&isAll=${isAll}"),
                title: "选择${title}",
                size: "small",
                buttons: {
                    cancel: {
                        label: "关闭",
                        callback: function () {
                            $("#${id}Id").val("");
                            $("#${id}Name").val("");
                            stationSelect($('#officeId').val());
                        }
                    },
                    ok: {
                        label: "确定",
                        callback: function () {
                            var innerIframe = $(document).find("iframe")[0];
                            if(innerIframe == null) return false;
                            var message_pos = $(innerIframe).contents().find(".alert-danger")[0];
                            var tree = innerIframe.contentWindow.tree;
                            var ids = [], names = [], nodes = [];
                            if ("${checked}" == "true") {
                                nodes = tree.getCheckedNodes(true);
                            } else {
                                nodes = tree.getSelectedNodes();
                            }
                            for (var i = 0; i < nodes.length; i++) {
                                //<c:if test="${checked && notAllowSelectParent}">
                                    if (nodes[i].isParent) {
                                        continue; // 如果为复选框选择，则过滤掉父节点
                                    }
                                //</c:if>
                                <c:if test="${officeGrade}">
                                if (nodes[i].grade != 4) {
                                    message_pos.style.display = "block";
                                    message_pos.innerHTML = "（" + nodes[i].name + ")不是有效站点&nbsp;请选择正确站点。";
                                    return false;//保证只能选择4级站点
                                }//</c:if>
                                <c:if test="${!notAllowSelectRoot}">
                                if (nodes[i].level == 0) {
                                    message_pos.style.display = "block";
                                    message_pos.innerHTML = "不能选择根节点（" + nodes[i].name + "）请重新选择。";
                                    return false;
                                }//</c:if><c:if test="${notAllowSelectParent}">
                                if (nodes[i].isParent) {
                                    message_pos.style.display = "block";
                                    message_pos.innerHTML = "不能选择父节点（" + nodes[i].name + "）请重新选择。";
                                    return false;
                                }//</c:if><c:if test="${not empty module && selectScopeModule}">
                                if (nodes[i].module == "") {
                                    message_pos.style.display = "block";
                                    message_pos.innerHTML = "不能选择公共模型（" + nodes[i].name + "）请重新选择。";
                                    return false;
                                } else if (nodes[i].module != "${module}") {
                                    message_pos.style.display = "block";
                                    message_pos.innerHTML = "不能选择当前栏目以外的栏目模型，请重新选择。";
                                    return false;
                                }//</c:if>
                                ids.push(nodes[i].id);
                                names.push(nodes[i].name);//<c:if test="${!checked}">
                                break; // 如果为非复选框选择，则返回第一个选择  </c:if>
                            }
                            if(ids.length == 0){
                                message_pos.style.display = "block";
                                message_pos.innerHTML = "您尚未做出选择,请先择选再\"确认\"!";
                                return false;
                            }else{
                                $("#${id}Id").val(ids.join(",").replace(/u_/ig, ""));
                                $("#${id}Name").val(names.join(","));
                                stationSelect($('#officeId').val());
                            }
                        }
                    }
                }
            });
        });

        function getIframe(url) {
            var newIframeObject = document.createElement("iframe");
            newIframeObject.src = url;
            newIframeObject.scrolling = "no";
            newIframeObject.frameBorder = 0;
            newIframeObject.width = "100%";
            newIframeObject.height = "400px";
            newIframeObject.style.border = "none";
            return newIframeObject;
        }
    });
</script>